using System;
using System.Collections.Generic;
using System.Text;

namespace GriffinNav.GPS
{
    public class Velocity
    {
        private decimal mSpeed = 0.0M;
        private decimal mAngle = 0.0M;

        internal Velocity()
        {

        }

        internal Velocity(decimal speedInKnots, decimal angle)
        {
            mSpeed = speedInKnots;
            mAngle = angle;
        }

        public decimal MPH
        {
            get
            {
                // convert to miles per hour
                return Conversion.KnotsToMph(mSpeed);
            }
        }
        public decimal KPH
        {
            get
            {
                // convert to kilometers per hour
                return Conversion.KnotsToKph(mSpeed);
            }
        }

        public int Angle
        {
            get
            {
                return Convert.ToInt32(mAngle);
            }
        }

        public VelocityDirection Direction
        {
            get
            {
                // North
                if ((Angle >= 0M && Angle < 11.25M) ||
                    (Angle >= 348.75M && Angle < 360M))
                {
                    return VelocityDirection.N;
                }

                // North Northeast
                if (Angle >= 11.25M && Angle < 33.75M)
                {
                    return VelocityDirection.NNE;
                }

                // Northeast
                if (Angle >= 33.75M && Angle < 56.25M)
                {
                    return VelocityDirection.NE;
                }

                // East Northeast
                if (Angle >= 56.25M && Angle < 78.75M)
                {
                    return VelocityDirection.ENE;
                }

                // East
                if (Angle >= 78.75M && Angle < 101.25M)
                {
                    return VelocityDirection.E;
                }

                // East Southeast
                if (Angle >= 101.25M && Angle < 123.75M)
                {
                    return VelocityDirection.ESE;
                }

                // Southeast
                if (Angle >= 123.75M && Angle < 146.25M)
                {
                    return VelocityDirection.SE;
                }

                // South Southeast
                if (Angle >= 146.25M && Angle < 168.75M)
                {
                    return VelocityDirection.SSE;
                }

                // South
                if (Angle >= 168.75M && Angle < 191.25M)
                {
                    return VelocityDirection.S;
                }

                // South Southwest
                if (Angle >= 191.25M && Angle < 213.75M)
                {
                    return VelocityDirection.SSW;
                }

                // Southwest
                if (Angle >= 213.75M && Angle < 236.25M)
                {
                    return VelocityDirection.SW;
                }

                // West Southwest
                if (Angle >= 236.25M && Angle < 258.75M)
                {
                    return VelocityDirection.WSW;
                }

                // West
                if (Angle >= 258.75M && Angle < 281.25M)
                {
                    return VelocityDirection.W;
                }

                // West Northwest
                if (Angle >= 281.25M && Angle < 303.75M)
                {
                    return VelocityDirection.WNW;
                }

                // Northwest
                if (Angle >= 303.75M && Angle < 326.25M)
                {
                    return VelocityDirection.NW;
                }

                // North Northwest
                if (Angle >= 326.25M && Angle < 348.75M)
                {
                    return VelocityDirection.NNW;
                }

                // default to North
                return VelocityDirection.N;
            }
        }

        internal void Update(decimal speedInKnots, decimal angle)
        {
            mSpeed = speedInKnots;
            mAngle = angle;
        }
    }
}
